package org.bing.engine.utility.codec;

import java.io.IOException;
import java.io.Writer;

public class SimpleXmlWriter {
	private Writer out;

	public SimpleXmlWriter(Writer out) {
		this.out = out;
	}

	public void beginElement(String en) throws IOException {
		out.write("<");
		out.write(en);
	}

	public void endElement(String en) throws IOException {
		out.write("</");
		out.write(en);
		out.write(">");
	}

	public void fastEndElement() throws IOException {
		out.write("/>");
	}

	public void addAttribute(String an, Object av) throws IOException {
		out.write(" ");
		out.write(an);
		out.write("=\"");
		out.write(av == null ? "null" : av.toString());
		out.write("\"");
	}

	public void addNotNullAttribute(String an, Object av) throws IOException {
		if (av == null) {
			return;
		} else {
			out.write(" ");
			out.write(an);
			out.write("=\"");
			out.write(av.toString());
			out.write("\"");
		}
	}

	public void endAttribute() throws IOException {
		out.write(">");
	}

	public void addText(String txt) throws IOException {
		out.write(txt);
	}

	public void addNull() throws IOException {
		out.write("<null />");
	}

	public void flush() throws IOException {
		out.flush();
	}

}
